home *** CD-ROM | disk | FTP | other *** search
/ The Very Best of Atari Inside / The Very Best of Atari Inside 1.iso / mint / mntlib43 / mntlib / ioctl.c < prev    next >
C/C++ Source or Header  |  1993-11-02  |  3KB  |  133 lines

  1. /*
  2.  * ioctl() emulation for MiNT; written by Eric R. Smith and placed
  3.  * in the public domain
  4.  */
  5.  
  6. #include <errno.h>
  7. #include <mintbind.h>
  8. #include <ioctl.h>
  9. #include <fcntl.h>
  10. #include <unistd.h>
  11. #include <linea.h>    /* for TIOCGWINSZ under TOS */
  12. #include <support.h>
  13. #include "lib.h"    /* for __open_stat */
  14.  
  15. extern int __mint;    /* MiNT version */
  16. int _ttydisc = NTTYDISC;
  17. int _ldisc = LLITOUT;
  18.  
  19. /* in read.c */
  20. extern struct tchars __tchars;
  21. extern struct ltchars __ltchars;
  22.  
  23. int ioctl(fd, cmd, arg)
  24.     int fd, cmd;
  25.     void *arg;
  26. {
  27.     long r;
  28.     int istty = isatty(fd);
  29.     struct sgttyb *sg = (struct sgttyb *) arg;
  30.     int null_fd;
  31.  
  32.     if (istty) {
  33.         if (cmd == TIOCGETD) {
  34.             *((int *)arg) = _ttydisc;
  35.             return 0;
  36.         } else if (cmd == TIOCSETD) {
  37.             _ttydisc = *((int *)arg);
  38.             return 0;
  39.         } else if (cmd == TIOCLGET) {
  40.             *((int *)arg) = _ldisc;
  41.             return 0;
  42.         } else if (cmd == TIOCLSET) {
  43.             _ldisc = *((int *)arg);
  44.             return 0;
  45.         } else if (cmd == TIOCSWINSZ && __mint < 9) {
  46.             return 0;
  47.         } else if (cmd == TIOCGWINSZ && __mint < 9) {
  48.             struct winsize *win = (struct winsize *)arg;
  49.  
  50. #ifndef __SOZOBON__
  51.             (void)linea0();
  52. #else /* __SOZOBON__ */
  53.             linea0();
  54. #endif /* __SOZOBON__ */
  55.             win->ws_row = V_CEL_MY + 1;
  56.             win->ws_col = V_CEL_MX + 1;
  57.             win->ws_xpixel = V_X_MAX;
  58.             win->ws_ypixel = V_Y_MAX;
  59.             return 0;
  60.         }
  61. #ifdef __MINT__
  62.         else if (cmd == TIOCNOTTY && __mint) {
  63.             if ((fd < 0) || !(_isctty(fd))) {
  64.                 errno = EBADF;
  65.                 return -1;
  66.             }
  67.             (void) Fclose(fd);
  68.             null_fd = (int) Fopen(__mint < 9 ? "V:\\null"
  69.                     : "U:\\dev\\null", O_RDWR);
  70.             (void) Fforce(-1, null_fd);
  71.             __open_stat[__OPEN_INDEX(-1)].status = FH_UNKNOWN;
  72.             __open_stat[__OPEN_INDEX(fd)].status = FH_UNKNOWN;
  73.             if (null_fd != fd) {
  74.                 (void) Fforce(fd, null_fd);
  75.                 (void) Fclose(null_fd);
  76.             }
  77.             return 0;
  78.         }
  79. #endif /* __MINT__ */
  80.     }
  81.  
  82.     if (__mint) {
  83.         r = Fcntl(fd, arg, cmd);
  84.     }
  85.     else if (istty) {
  86.         r = 0;
  87.         switch(cmd) {
  88.         case TIOCSETP:
  89.             fd = __OPEN_INDEX(fd);
  90.             if (fd < 0 || fd >= __NHANDLES)
  91.                 fd = __NHANDLES - 1;
  92.             __open_stat[fd].flags = sg->sg_flags;
  93.             break;
  94.         case TIOCGETP:
  95.             fd = __OPEN_INDEX(fd);
  96.             if (fd < 0 || fd >= __NHANDLES)
  97.                 fd = __NHANDLES - 1;
  98.             sg->sg_flags = __open_stat[fd].flags;
  99.             sg->sg_ispeed = sg->sg_ospeed = 0;
  100.             sg->sg_erase = 'H' & 0x1f;
  101.             sg->sg_kill = 'U' & 0x1f;
  102.             break;
  103.         case TIOCGETC:
  104.             *((struct tchars *)arg) = __tchars;
  105.             break;
  106.         case TIOCSETC:
  107.             __tchars = *((struct tchars *)arg);
  108.             break;
  109.         case TIOCGLTC:
  110.             *((struct ltchars *)arg) = __ltchars;
  111.             break;
  112.         case TIOCSLTC:
  113.             __ltchars = *((struct ltchars *)arg);
  114.             break;
  115.         case TIOCGPGRP:
  116.             *((long *)arg) = 0;
  117.             break;
  118.         case TIOCSPGRP:
  119.             break;
  120.         default:
  121.             r = -EINVAL;
  122.         }
  123.     }
  124.     else
  125.         r = -EINVAL;
  126.  
  127.     if (r < 0) {
  128.         errno = (int) -r;
  129.         return -1;
  130.     }
  131.     return (int) r;
  132. }
  133.